package com.member.web.controller.shop;

import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizCouponGrant;
import com.member.shop.service.ISysBizCouponGrantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【优惠券发放】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/grant")
public class SysBizCouponGrantController extends BaseController {
    @Resource
    private ISysBizCouponGrantService sysBizCouponGrantService;

    private void setTenantId(SysBizCouponGrant sysBizCouponGrant) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizCouponGrant.setTenantId(tenantId);
        }
    }

    /**
     * 查询优惠券发放列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizCouponGrant sysBizCouponGrant) {
        startPage();
        setTenantId(sysBizCouponGrant);
        List<SysBizCouponGrant> list = sysBizCouponGrantService.selectSysBizCouponGrantList(sysBizCouponGrant);
        return getDataTable(list);
    }

    /**
     * 导出优惠券发放列表
     */
    @PreAuthorize("@ss.hasPermi('shop:grant:export')")
    @Log(title = "优惠券发放", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizCouponGrant sysBizCouponGrant) {
        setTenantId(sysBizCouponGrant);
        List<SysBizCouponGrant> list = sysBizCouponGrantService.selectSysBizCouponGrantList(sysBizCouponGrant);
        ExcelUtil<SysBizCouponGrant> util = new ExcelUtil<SysBizCouponGrant>(SysBizCouponGrant.class);
        util.exportExcel(response, list, "优惠券发放数据");
    }

    /**
     * 获取优惠券发放详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:grant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizCouponGrantService.selectSysBizCouponGrantById(id));
    }

    /**
     * 新增优惠券发放
     */
    @PreAuthorize("@ss.hasPermi('shop:grant:add')")
    @Log(title = "优惠券发放", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizCouponGrant sysBizCouponGrant) {
        try {
            int rows = sysBizCouponGrantService.insertSysBizCouponGrant(sysBizCouponGrant);
            return toAjax(rows);
        } catch (Exception e) {
            e.printStackTrace();

            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改优惠券发放
     */
    @PreAuthorize("@ss.hasPermi('shop:grant:edit')")
    @Log(title = "优惠券发放", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizCouponGrant sysBizCouponGrant) {
        return toAjax(sysBizCouponGrantService.updateSysBizCouponGrant(sysBizCouponGrant));
    }

    /**
     * 删除优惠券发放
     */
    @PreAuthorize("@ss.hasPermi('shop:grant:remove')")
    @Log(title = "优惠券发放", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizCouponGrantService.deleteSysBizCouponGrantByIds(ids));
    }
}
